package club.emergency.wechat.staff.operate.consumer.controller;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.model.staff.Staff;
import club.emergency.wechat.staff.operate.consumer.service.StaffOperateConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create with IntelliJ IDEA
 * Project Name: wecaht
 * Package Name: club.emergency.wechat.staff.operate.consumer.controller
 * Date: 2018/11/16
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("用户增删改模块")
@RestController
@RequestMapping("/staff-operate")
public class StaffOperateController {

    private StaffOperateConsumer staffOperateConsumer;

    public StaffOperateController(StaffOperateConsumer staffOperateConsumer) {
        this.staffOperateConsumer = staffOperateConsumer;
    }

    @ApiOperation("新增人员")
    @PostMapping("/save")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "姓名", name = "userName", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "微信openId", name = "openId", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "电话号", name = "phone", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "昵称", name = "nickname", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "头像", name = "headimgurl", dataType = "string", paramType = "query")
    })
    public ResponseEntity save(
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam("openId") String openId,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "headimgurl", required = false) String headimgurl
    ) {
        Staff staff = new Staff();
        staff.setUserName(userName);
        staff.setOpenId(openId);
        staff.setPhone(phone);
        staff.setNickname(nickname);
        staff.setHeadimgurl(headimgurl);
        staff.setSignIn(0);
        staff.setEachTest(0);
        return new JsonConvert<ResponseEntity>().jsonDecode(staffOperateConsumer.save(staff), ResponseEntity.class);
    }

    @ApiOperation("修改人员")
    @PostMapping("/modify")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(value = "姓名", name = "userName", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "微信openId", name = "openId", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "电话号", name = "phone", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "每日答题状态", name = "eachTest", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "昵称", name = "nickname", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "头像", name = "headimgurl", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "当前用户选中的试题字典类型", name = "currentQuestionDictionaryAlias", dataType = "string", paramType = "query")
    })
    public ResponseEntity modify(
            @RequestParam(name = "id", required = false) Long id,
            @RequestParam(name = "userName", required = false) String userName,
            @RequestParam(name = "openId", required = false) String openId,
            @RequestParam(name = "phone", required = false) String phone,
            @RequestParam(name = "eachTest", required = false) Integer eachTest,
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "headimgurl", required = false) String headimgurl,
            @RequestParam(name = "currentQuestionDictionaryAlias", required = false) String currentQuestionDictionaryAlias
    ) {
        Staff staff = new Staff();
        staff.setId(id);
        staff.setUserName(userName);
        staff.setOpenId(openId);
        staff.setPhone(phone);
        staff.setEachTest(eachTest);
        staff.setNickname(nickname);
        staff.setHeadimgurl(headimgurl);
        staff.setCurrentQuestionDictionaryAlias(currentQuestionDictionaryAlias);
        return new JsonConvert<ResponseEntity>().jsonDecode(staffOperateConsumer.modify(staff), ResponseEntity.class);
    }

    @ApiOperation("签到，（每天重置），0为未签到，1为已签到。默认为0")
    @PostMapping("/signIn")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(value = "openId", name = "openId", required = true, dataType = "string", paramType = "query")
    })
    public ResponseEntity signIn(
            @RequestParam("id") Long id, @RequestParam("openId") String openId
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(staffOperateConsumer.signIn(id, openId), ResponseEntity.class);
    }

    @ApiOperation("微信手机号解绑")
    @PostMapping("/unBind")
    @ApiImplicitParam(value = "openId", name = "openId", required = true, dataType = "string", paramType = "query")
    public ResponseEntity unBind(
            @RequestParam("openId") String openId
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(staffOperateConsumer.unBind(openId), ResponseEntity.class);
    }

    @ApiOperation("更新每日答题的状态")
    @PostMapping("/updateEachTest")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(value = "openId", name = "openId", required = true, dataType = "string", paramType = "query")
    })
    public ResponseEntity updateEachTest(
            @RequestParam("id") Long id, @RequestParam("openId") String openId
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(staffOperateConsumer.updateEachTest(id, openId), ResponseEntity.class);
    }
}
